<?php

/**
 * 会员控制器
 */
class Controller_Member extends Controller_Abstract
{
    function actionIndex()
    {
       
    }

    function actionList(){
    
    	$page = $this->_context->param('page',1);
    	$page_size = $this->_context->param('row',10);
    	$conditions=' 1=1 ';
    	if($this->_context->post('key') && $this->_context->post('op') && $this->_context->post('value') ){
    		switch ($this->_context->post('op')){
    			case 'like':
    				$conditions = $this->_context->post('key').' like "%'.$this->_context->post("value").'%" ';
    				break;
    			default:
    				$conditions = $this->_context->post('key').$this->_context->post('op')."'".$this->_context->post('value')."'";	
    		}	
    	}
    	
    	if($this->_context->post('site_id')){
    		$conditions.=' and site_id='.$this->_context->post('site_id');
    	}
    	if($this->_context->post('agent_id')){
    		$conditions.=' and agent_id='.$this->_context->post('agent_id');
    	}
    	if($this->_context->post('keyword')){
    		$conditions.=' and (name like "%'.$this->_context->post('keyword').'%" or username like "%'.$this->_context->post('keyword').'%")';
    	}
    	$list = Member::find($conditions)->asArray()->limitPage($page, $page_size)->getAll();
    	
    	$rowset = Bank::find("status=0")->asArray()->getAll();
    	$bank=Helper_Array::toHashmap($rowset, 'bank_id','name');
    	
    	$rowset = Agent::find("status=0")->asArray()->getAll();
    	$agent=Helper_Array::toHashmap($rowset, 'agent_id','name');
    	
    	
    	$rowset = site::find("status=0")->asArray()->getAll();
    	$site=Helper_Array::toHashmap($rowset, 'site_id','name');
    	
    	$result=array();
    	if($list){
    		foreach($list as $key => $val){
    			$row=$val;
    			$row['agent']=$agent[$val['agent_id']];
    			$row['bank']=$bank[$val['bank_id']];
    			$row['site']=$site[$val['site_id']];
    			$result[]=$row;
    		}
    	}
    	return json_encode($result);
    
    }
    
    function actionNew(){
    	
    	$member = new Member($_POST);
    	
	    try {
	    	
	    	$member->save();
	    	
			return json_encode(array('success'=>true));
			
		} catch (QDB_ActiveRecord_ValidateFailedException $e) {			
			
			return json_encode(array('errorMsg' => $e->getMessage()));
			
		}
    	catch (Exception $e){
    		return json_encode(array('errorMsg'=>'系统出错,操作未能完成.'.$e->getMessage()));
    	}
    }
    function actionUpdate(){
    	$id=$this->_context->post('member_id');
    	$member = Member::find('member_id=?',$id)->getOne();
    	
    	
    	
    	try{
    		$member->changeProps($_POST);
    		$member->save();
    		return  json_encode(array('success'=>true));
    	}
    	catch (QDB_ActiveRecord_ValidateFailedException $e) {			
			
			return json_encode(array('errorMsg' => $e->getMessage()));
			
		}
    	catch (Exception $e){
    		return json_encode(array('errorMsg'=>'系统出错,操作未能完成.'));
    	}
    }
    
    function actionDestroy(){
    	
    	$id=$this->_context->post('id',0);
    	$row=Member::find("member_id=?",$id)->getOne();

    	try {
    		$row->destroy();
			return  json_encode(array('success'=>true));
    	}
    	catch (Exception $e){
				return json_encode(array('errorMsg'=>'系统出错,操作未能完成.'));
		}
    }
}

